package com.friends.utils.validate.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public final class EnvDetector implements InitializingBean {
    @Resource
    private Environment environment;

    /**
     * 是否是开发环境 , test 是测试
     */
    public boolean isTest() {
        return ArrayUtils.contains(environment.getActiveProfiles(), ConditionalEnum.TEST.getEnvName());
    }

   /* *//**
     * 是否是 uat
     *//*
    public boolean isUat() {
        return ArrayUtils.contains(environment.getActiveProfiles(), ConditionalOnRegion.UAT);
    }*/

    /**
     * 是否是 线上
     */
    public boolean isProd() {
        return ArrayUtils.contains(environment.getActiveProfiles(), ConditionalEnum.PROD.getEnvName());
    }

    /**
     * 是否是测试环境 , dev 是开发环境
     */
    public boolean isDev() {
        return ArrayUtils.contains(environment.getActiveProfiles(), ConditionalEnum.DEV.getEnvName());
    }

    /**
     * 获取环境名称
     * <dl>
     * <dt>开发环境</dt>
     * <dd>test</dd>
     * <dt>测试环境</dt>
     * <dd>dev</dd>
     * <dt>云线上</dt>
     * <dd>prd</dd>
     * <dt>本地化部署</dt>
     * <dd>liwan or dongguan or danyang</dd>
     * </dl>
     */
    public String envName() {
        if (isDev()) {
            return ConditionalEnum.DEV.getEnvName();
        } else if (isTest()) {
            return ConditionalEnum.TEST.getEnvName();
        }else if (isProd()) {
            return ConditionalEnum.PROD.getEnvName();
        } else {
            throw new IllegalStateException("环境错误");
        }
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("当前环境: {}", envName());
    }

    public enum ConditionalEnum {
        DEV("dev"),
        TEST("test"),
        PROD("prd");

        private String envName;

        ConditionalEnum(String envName) {
            this.envName = envName;
        }

        public String getEnvName() {
            return envName;
        }
    }
}